package org.android.sss;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.android.maps.Road;
import org.android.maps.RoadProvider;
import org.android.tools.Global;
import org.android.tools.Shop;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;





public class ShowOnMapActivity extends MapActivity {
	
	Canvas canvastmp;
	LinearLayout linearLayout;
	MapView mapView;
	private List<Road> roads;
	private List<GeoPoint> points;
	private List<Overlay> listOfOverlays;
	private List<Shop> choosenShops;
	
	
	int dupa = 0;
	
	
	//private MapLocationOverlay overlay;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_on_map);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.setClickable(true);
		listOfOverlays = mapView.getOverlays();
		points = new ArrayList<GeoPoint>();
		roads = new ArrayList<Road>();
		
		
		choosenShops = new ArrayList<Shop>();
		choosenShops.add(Global.productsShopsShoppingList[0].shop);
		Boolean isFound = false;
        for (int i = 0; i < Global.productsShopsShoppingList.length; i++)
		{
            isFound = false;
            for (int j = 0; j < choosenShops.size(); j++)
            {
		        if (choosenShops.get(j).idshop == Global.productsShopsShoppingList[i].shop.idshop) 
                {
                    isFound = true;
                    break;
                }
		    }
            if (isFound == false) 
            {
                choosenShops.add(Global.productsShopsShoppingList[i].shop);
            }
        }
        
        Global.choosenShops = choosenShops;
        Global.markersName = new ArrayList<String>();
        for (int i = 0; i < choosenShops.size(); i++)
        {
            Global.markersName.add("");
        }
        
        for (int i = 0; i < Global.productsShopsShoppingList.length; i++)
        {
            for (int j = 0; j < choosenShops.size(); j++)
            {
                if (Global.productsShopsShoppingList[i].shop.idshop == choosenShops.get(j).idshop)
                {
                	Global.markersName.set(j, Global.markersName.get(j) + " \n" + Global.productsShopsShoppingList[i].product.name +
                            " " + Global.productsShopsShoppingList[i].product.price.toString() + " DKK " + Global.productsShopsShoppingList[i].product.brand +
                            " " + Global.productsShopsShoppingList[i].product.howPacket);
                }
            }
        }
        Global.markersName.add(0, "You are HERE!");
        for (int i = 0; i < choosenShops.size(); i++) {
        	Integer number = choosenShops.get(i).number;
        	Global.markersName.set(i+1, choosenShops.get(i).name + 
        			"\n"+choosenShops.get(i).street+" " + number.toString()+"\n"+
        			Global.markersName.get(i+1));
        }
        
        double x11 = Global.xCoordinate;
        double y11 = Global.yCoordinate;
        drawPoints(x11,y11);
        for (int i = 0; i < choosenShops.size(); i++) {
        	double x22 = choosenShops.get(i).x;
        	double y22 = choosenShops.get(i).y;
        	drawPoints(x22, y22);
        }
        double x1 = Global.xCoordinate;
        double x2 = choosenShops.get(0).x;
        double y2 = choosenShops.get(0).y;
        double y1 = Global.yCoordinate;
        getTheRoad(x1, y1, x2, y2);
        for (int i = 1; i < choosenShops.size(); i++) {
        	getTheRoad(choosenShops.get(i-1).x, choosenShops.get(i-1).y, choosenShops.get(i).x, choosenShops.get(i).y);
        }
		MapController mc = mapView.getController();
		mc.animateTo(points.get(0));
		mc.setZoom(17); 

		//---Add a location marker---
		MapOverlayP mapOverlayp = new MapOverlayP();

		//listOfOverlays.clear();
		listOfOverlays.add(mapOverlayp);
		
		mapView.invalidate();

	}

	public void drawPoints(double x, double y){
			GeoPoint p = new GeoPoint((int)(x*1E6),(int)(y*1E6));
			points.add(p);
	}


	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			
			MapOverlay mapOverlay;
			for (int i = 0; i < roads.size(); i++) {
				mapOverlay = new MapOverlay(roads.get(i), mapView);
				listOfOverlays.add(mapOverlay);
			}


		};
	};

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	private InputStream getConnection(String url) {
		InputStream is = null;
		try {
			URLConnection conn = new URL(url).openConnection();
			is = conn.getInputStream();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return is;
	}
	private void getTheRoad(double x1, double y1, double x2, double y2){

		String url = RoadProvider.getUrl(x1, y1, x2, y2);
		InputStream is = getConnection(url);
		//if (dupa == 0) {
			//mRoad = RoadProvider.getRoute(is);
			//roads.add(RoadProvider.getRoute(is));
		//}
		//if (dupa == 1) {
			//mRoad2 = RoadProvider.getRoute(is);
			roads.add(RoadProvider.getRoute(is));
		//}
		//Road tmpRoad = RoadProvider.getRoute(is);
		
		//roads.add(tmpRoad);
		
		//if (roads.size() == choosenShops.size())
		mHandler.sendEmptyMessage(0);

	}

	class MapOverlayP extends com.google.android.maps.Overlay
	{
		Bitmap bmp;
		List<Point> screenPts;
		@Override
		public boolean draw(Canvas canvas, MapView mapView, 
				boolean shadow, long when) 
		{
			
			super.draw(canvas, mapView, shadow);
			screenPts = new ArrayList<Point>();
			for (int i = 0; i < points.size(); i++) {
				Point screenPts2 = new Point();
				mapView.getProjection().toPixels(points.get(i), screenPts2);
				screenPts.add(screenPts2);
				//---add the marker---
				bmp = BitmapFactory.decodeResource(
						getResources(), R.drawable.marker);            
				canvas.drawBitmap(bmp, screenPts2.x, screenPts2.y-34, null);
			}
			//---translate the GeoPoint to screen pixels---
			return true;
		}
		public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
             if(event.getAction() == 1) {
            	int x= (int) event.getX();
                int y=(int) event.getY();
               
                for (int i = 0; i < screenPts.size(); i++) {
                	if((x>screenPts.get(i).x-34) && (x<screenPts.get(i).x+bmp.getWidth()+34) && (y>screenPts.get(i).y-34-34) && (y<screenPts.get(i).y-34+bmp.getHeight()+34)){
                			Toast.makeText(getBaseContext(), 
                                    Global.markersName.get(i), 
                                    Toast.LENGTH_LONG).show();
                		
                   
                    }
                }
             }
                
            return false;
        } 
		
	} 
}

class MapOverlay extends com.google.android.maps.Overlay {
	Road mRoad;
	ArrayList<GeoPoint> mPoints;

	public MapOverlay(Road road, MapView mv) {
		mRoad = road;
		if (road.mRoute.length > 0) {
			mPoints = new ArrayList<GeoPoint>();
			for (int i = 0; i < road.mRoute.length; i++) {
				mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
						(int) (road.mRoute[i][0] * 1000000)));
			}
			int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
					mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
					.getLatitudeE6()) / 2);
			int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
					mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
					.getLongitudeE6()) / 2);
			GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

			//MapController mapController = mv.getController();
			//mapController.animateTo(moveTo);
			//mapController.setZoom(7);
		}
	}

	@Override
	public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
		super.draw(canvas, mv, shadow);
		drawPath(mv, canvas);
		return true;
	}

	public void drawPath(MapView mv, Canvas canvas) {
		int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
		Paint paint = new Paint();
		paint.setColor(Color.BLUE);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(3);
		for (int i = 0; i < mPoints.size(); i++) {
			Point point = new Point();
			mv.getProjection().toPixels(mPoints.get(i), point);
			x2 = point.x;
			y2 = point.y;
			if (i > 0) {
				canvas.drawLine(x1, y1, x2, y2, paint);
			}
			x1 = x2;
			y1 = y2;
		}
	}
}
